package com.zhonghan.recharge.service.impl;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhonghan.common.bean.Result;
import com.zhonghan.common.utils.StringUtils;
import com.zhonghan.recharge.dao.ApplyBuyCardOrderDao;
import com.zhonghan.recharge.pojo.ApplyBuyCardOrder;
import com.zhonghan.recharge.service.ApplyBuyCardOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ApplyBuyCardOrderServiceImpl implements ApplyBuyCardOrderService{
    @Autowired
    private ApplyBuyCardOrderDao applyBuyCardOrderDao;
    @Override
    public long getApplyBuyCardOrderRowCount(){
        return applyBuyCardOrderDao.getApplyBuyCardOrderRowCount();
    }
    @Override
    public List<ApplyBuyCardOrder> selectApplyBuyCardOrder(){
        return applyBuyCardOrderDao.selectApplyBuyCardOrder();
    }
    @Override
    public List<ApplyBuyCardOrder> selectApplyBuyCardOrderByObj(ApplyBuyCardOrder obj){
        return applyBuyCardOrderDao.selectApplyBuyCardOrderByObj(obj);
    }
    @Override
    public ApplyBuyCardOrder selectApplyBuyCardOrderById(Integer id){
        return applyBuyCardOrderDao.selectApplyBuyCardOrderById(id);
    }
    @Override
    public int insertApplyBuyCardOrder(ApplyBuyCardOrder value){
        return applyBuyCardOrderDao.insertApplyBuyCardOrder(value);
    }
    @Override
    public int insertNonEmptyApplyBuyCardOrder(ApplyBuyCardOrder value){
        return applyBuyCardOrderDao.insertNonEmptyApplyBuyCardOrder(value);
    }
    @Override
    public int insertApplyBuyCardOrderByBatch(List<ApplyBuyCardOrder> value){
        return applyBuyCardOrderDao.insertApplyBuyCardOrderByBatch(value);
    }
    @Override
    public int deleteApplyBuyCardOrderById(Integer id){
        return applyBuyCardOrderDao.deleteApplyBuyCardOrderById(id);
    }
    @Override
    public int updateApplyBuyCardOrderById(ApplyBuyCardOrder enti){
        return applyBuyCardOrderDao.updateApplyBuyCardOrderById(enti);
    }
    @Override
    public int updateNonEmptyApplyBuyCardOrderById(ApplyBuyCardOrder enti){
        return applyBuyCardOrderDao.updateNonEmptyApplyBuyCardOrderById(enti);
    }

	@Override
	public Result addApplyBuyCardOrder(String name, String telephone,String openId) {
		
		Result result = new Result();
		
		ApplyBuyCardOrder order = new ApplyBuyCardOrder();
		
		try {
			
			// 先查询数据库是否有记录,如果有记录则不允许购买
			ApplyBuyCardOrder obj = new ApplyBuyCardOrder();
			obj.setApplyName(name);
			List<ApplyBuyCardOrder> list = applyBuyCardOrderDao.selectApplyBuyCardOrderByObj(obj);
			if(list != null && list.size() > 0){
				result.setFailure("0", "您已参加过该活动,不允许重复购买!");
				return result;
			}
			// 再次根据手机号码进行判断
			ApplyBuyCardOrder obj2 = new ApplyBuyCardOrder();
			obj2.setApplyTelephone(telephone);
			List<ApplyBuyCardOrder> list2 = applyBuyCardOrderDao.selectApplyBuyCardOrderByObj(obj2);
			if(list2 != null && list2.size() > 0){
				result.setFailure("0", "您已参加过该活动,不允许重复购买!");
				return result;
			}
			
			order.setApplyName(name);
			order.setApplyTelephone(telephone);
			order.setOpenId(openId);
			order.setOrderTime(new Date());
			
			order.setOrderStatus(0);
			
			int rs = applyBuyCardOrderDao.insertNonEmptyApplyBuyCardOrder(order);
			if(rs > 0){
				result.setSuccess("1", null, "提交成功,活动相关内容工作人员稍后会联系到您!");
			} else {
				result.setFailure("0", "订单提交失败,请重试!");
			}
			
		} catch (Exception e) {
			result.setFailure("-1", "系统繁忙,请稍后再试!");
			e.printStackTrace();
		}
		
		return result;
	}
	@Override
	public Result getApplyBuyCardOrderByCondition(Integer pageNum,Integer pageSize,Integer id, Integer orderStatus, String telephone,String beginTime,String endTime) {
		pageNum = pageNum == null ? 1 : pageNum;
		pageSize = pageSize == null ? 10 : pageSize;
		Result result = new Result();
		
		ApplyBuyCardOrder order = new ApplyBuyCardOrder();
		order.setId(id);
		order.setOrderStatus(orderStatus);
		if(StringUtils.isNotEmpty(telephone)){
			order.setApplyTelephone(telephone);
		}
		if(StringUtils.isNotEmpty(beginTime)){
			order.setBeginTime(beginTime);
		}
		if(StringUtils.isNotEmpty(endTime)){
			order.setEndTime(endTime);
		}
		
		try {
			PageHelper.startPage(pageNum, pageSize);
			List<ApplyBuyCardOrder> list = applyBuyCardOrderDao.selectApplyBuyCardOrderByCondition(order);
			PageInfo<ApplyBuyCardOrder> pageInfo = new PageInfo<>(list);
			
			result.setSuccess("1", pageInfo, null);
			
		} catch (Exception e) {
			result.setFailure("-1", "系统繁忙,请稍后再试!");
			e.printStackTrace();
		}
		
		return result;
	}
	@Override
	public Result updateApplyBuyCard(Integer id, BigDecimal payAmount, BigDecimal donationAmount, String cardSegment,
			Integer isPay, Integer orderStatus) {
		
		Result result = new Result();
		
		try {
			
			ApplyBuyCardOrder order = new ApplyBuyCardOrder();
			if(id != null){
				order.setId(id);
			}
			if(payAmount != null){
				order.setPayAmount(payAmount);
			}
			if(donationAmount != null){
				order.setDonationAmount(donationAmount);
			}
			if(StringUtils.isNotEmpty(cardSegment)){
				order.setCardSegment(cardSegment);
			}
			if(payAmount != null && donationAmount != null){
				order.setTotalAmount(payAmount.add(donationAmount));
			}
			
			int rs = applyBuyCardOrderDao.updateNonEmptyApplyBuyCardOrderById(order );
			
			if(rs > 0){
				result.setSuccess("1", null, "更新成功!");
			}else{
				result.setFailure("0", "更新失败!");
			}
			
			
		} catch (Exception e) {
			result.setFailure("-1", "系统繁忙,请稍后再试!");
			e.printStackTrace();
		}
		
		return result;
	}

}